﻿using _2DLineCamera.Models;
using HalconDotNet;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace _2DLineCamera
{
    public class Tool
    {
        public  void WriteCsv(List<Measure_Result> StationMeasureResult,string leftOrRight,string code,string result,string Product)
        {
            try
            {
                if (code == "")
                {
                    code = "未识别";
                }
                string path = "D://Data//" + leftOrRight;
                if (Directory.Exists(path) == false) Directory.CreateDirectory(path);
                FileStream fileStream = new FileStream(path + "//" + DateTime.Now.ToString("yyyyMMdd") + ".csv", FileMode.Append, FileAccess.Write);
                StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8);
                string Tilte = "产品型号,时间,SN,结果,";
                string Content = Product + "," + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "," + code + "," + result + ",";
                foreach (Measure_Result item in StationMeasureResult)
                {
                    if (item.Measure_Name != "")
                    {
                        Tilte = Tilte + item.Measure_Name + ",";
                        Content = Content + item.Distance.ToString("0.00") + ",";
                    }

                }
                streamWriter.WriteLine(Tilte);
                streamWriter.WriteLine(Content);
                streamWriter.Close();
                fileStream.Close();
            }
            catch (Exception ex)
            {

               
            }
            
        }
        public void SaveConfigFile(Config ConfigFile)
        {
            try
            {
                string Json_Config = JsonConvert.SerializeObject(ConfigFile, Formatting.Indented);
                FileStream fileStream1 = new FileStream(Directory.GetCurrentDirectory() + @"\Config\config.json", FileMode.Create, FileAccess.Write);
                StreamWriter streamWriter1 = new StreamWriter(fileStream1, Encoding.UTF8);
                if (Json_Config != "")
                {
                    streamWriter1.Write(Json_Config);
                }
                streamWriter1.Close();
                fileStream1.Close();

            }
            catch
            {
                MessageBox.Show("保存参数异常");
            }
        }
        public void SaveProductFile(ProductList ConfigFile)
        {
            try
            {
                string Json_Config = JsonConvert.SerializeObject(ConfigFile, Formatting.Indented);
                FileStream fileStream1 = new FileStream(Directory.GetCurrentDirectory() + @"\Config\ProductList.json", FileMode.Create, FileAccess.Write);
                StreamWriter streamWriter1 = new StreamWriter(fileStream1, Encoding.UTF8);
                if (Json_Config != "")
                {
                    streamWriter1.Write(Json_Config);
                }
                streamWriter1.Close();
                fileStream1.Close();

            }
            catch
            {
                MessageBox.Show("保存参数异常");
            }
        }

        public Config ReadConfigFile()
        {
            Config configFile = new Config();
            FileStream fileStream1 = new FileStream(Directory.GetCurrentDirectory() + @"\Config\config.json", FileMode.Open, FileAccess.Read);
            StreamReader streamReader1 = new StreamReader(fileStream1, Encoding.UTF8);
            String Json_Config = streamReader1.ReadToEnd();
            streamReader1.Close();
            fileStream1.Close();
            configFile = JsonConvert.DeserializeObject<Config>(Json_Config);
            return configFile;
        }
        public void WriteMesTxt(string MesSavePath, string Code, string MesJudge, List<Measure_Result> StationMeasureResult, PinCheck PinStationMeasureResult,bool Pincheck)
        {

            if (Directory.Exists(MesSavePath) == false)
            {
                Directory.CreateDirectory(MesSavePath);
            }
            FileStream mesfileStream = new FileStream(MesSavePath + "//" + Code + ".txt", FileMode.Create, FileAccess.Write);
            StreamWriter messtreamWriter = new StreamWriter(mesfileStream, Encoding.UTF8);
            string mesTilte = "";
            string mesContent = "";
            foreach (Measure_Result item in StationMeasureResult)
            {
                if (item.Measure_Name!="")
                {
                    mesContent = mesContent + item.Distance.ToString("0.00") + ",";
                }
                
            }
            if (Pincheck == true)
            {
                mesTilte = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "," + "AOI量测" + "," + MesJudge + "," + Code + "," + mesContent + PinStationMeasureResult.Height.ToString("f2") + "," + PinStationMeasureResult.Width.ToString("f2")
              + "," + PinStationMeasureResult.Row.ToString("f2") + "," + PinStationMeasureResult.Column.ToString("f2") + "," + PinStationMeasureResult.Height1.ToString("f2") + "," + PinStationMeasureResult.Width1.ToString("f2")
              + "," + PinStationMeasureResult.Row1.ToString("f2") + "," + PinStationMeasureResult.Column1.ToString("f2") + ",";
            }
            else
            {
                mesTilte = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "," + "AOI量测" + "," + MesJudge + "," + Code + "," + mesContent + ",";
            }
            messtreamWriter.WriteLine(mesTilte);
            messtreamWriter.Close();
            mesfileStream.Close();


        }
             
        /// <summary>
        /// 获取文件夹路径
        /// </summary>
        /// <returns></returns>
        public string OpenSaveImagePath()
        {
            try
            {
                string FolderPath = "";
                FolderBrowserDialog Dialog = new FolderBrowserDialog();
                Dialog.Description = "请选择文件路径";
                if (Dialog.ShowDialog() == DialogResult.OK)
                {
                    FolderPath = Dialog.SelectedPath;
                    if (string.IsNullOrWhiteSpace(FolderPath))
                    {
                        MessageBox.Show( "文件夹路径不能为空", "提示");
                        return "";
                    }
                    else
                    {
                        return FolderPath;
                    }
                }
                return FolderPath;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + e.InnerException);
                return "";
            }
        }
        /// <summary>
        /// 判断输入参数是否符合要求
        /// </summary>
        /// <param name="textBox"></param>
        /// <param name="Value"></param>
        private void SaveDataToTxtBox(TextBox textBox, ref double Value)
        {
            if (textBox.Text != null)
            {
                try
                {
                    Value = Convert.ToDouble(textBox.Text);
                }
                catch
                {
                    MessageBox.Show("请输入一个合理的数值");
                    textBox.Text = "0";
                    Value = 0;
                }
            }
        }

    }
    public class LineAndRecangelResult
    {
        public string name = "";
        public double startRow = 0;
        public double startCol = 0;
        public double endRow = 0;
        public double endCol = 0;

        public double centerRow = 0;
        public double centerCol = 0;
        public double radius = 0;

    }
   public class ProductList
    {
        public List<Product> Product_list = new List<Product>();
        public string Product_Now = "";
    }
    public class Product
    {
        public string ProductName = "";
        public string StandraModel_Path1 = "";
        public string StandraModel_Path2 = "";
        public string ModelRegion_Path1 = "";
        public string ModelRegion_Path2 = "";


        public double Standard_Row = 0;
        public double Standard_Column = 0;
        public double Standard_Angle = 0;

        public List<Feature> FeaturesList = new List<Feature>();
        public List<Measure_Tool> MeasureList = new List<Measure_Tool>();
        public List<Feature> CalibrationLineList = new List<Feature>();


        public string HikProductName = "";
        public string HikStandraModel_Path = "";
        public double HikStandraCenterX = 0;
        public double HikStandRaCenterY = 0;
        public bool Pinchek = false;
        public string LeftPinchekPos = "";
        public string RightPinchekPos = "";
    }


    public class Feature
    {
        public Feature_Style _Style;
        public string Region_Path = "";
        public H_Point LineStartPoint = new H_Point();
        public H_Point LineEndPoint = new H_Point();
        public string Name = "";
    }
    public enum Feature_Style
    {
        Circle = 0,
        Line=1,
        ReadCode=2,
        SemiCircle=3,
        ellipse=4,
        LineRC,
        LineXY
    }

    public enum Code_Style
    {
        QR_Code1 = 0,
        QR_Code2 = 1,
        bar_Code=2,
    }

    public class Measure_Result
    {
        public bool Judge = false;
        public string Code = "";
        public double Distance = 0;
        public string Measure_Name = "";
        public double Width = 0;

    }
    public class Measure
    {
        HObject region = new HObject();
        public Measure(HTuple width,HTuple height)
        {
            HOperatorSet.GenImageConst(out  region, "byte", width, height);
        }
        public void GetResult(HSmartWindowControl hSmartWindowControl1, Feature feature, HObject image, HTuple HomMat2DTranslate, string path, List<LineAndRecangelResult> lineAndRecangelResults)
        {
            try
            {
                //HOperatorSet.GetImageSize(image, out HTuple width, out HTuple height);
                //HOperatorSet.GenImageConst(out HObject region, "byte", width, height);
                HOperatorSet.ReadRegion(out  region, path);
                HOperatorSet.AffineTransRegion(region, out HObject affine_Region1, HomMat2DTranslate, "nearest_neighbor");
                HOperatorSet.SetColor(hSmartWindowControl1.HalconWindow, "blue");
                HOperatorSet.SetDraw(hSmartWindowControl1.HalconWindow, "margin");
                HOperatorSet.DispObj(affine_Region1, hSmartWindowControl1.HalconWindow);
                if (feature.Name.Contains("区域")&& !feature.Name.Contains("基准轴") && !feature.Name.Contains("点构线"))
                {
                    Algorithm.Get_Radius(image, affine_Region1, out HObject Circle1, out HTuple Radius, out HTuple Row1, out HTuple Column1);
                    HOperatorSet.SetColor(hSmartWindowControl1.HalconWindow, "red");
                    HOperatorSet.DispObj(Circle1, hSmartWindowControl1.HalconWindow);
                    lineAndRecangelResults.Add(new LineAndRecangelResult() { name = feature.Name, centerRow = Row1.D, centerCol = Column1.D, radius = Radius.D });
                }
                else if (feature.Name.Contains("直线") && !feature.Name.Contains("基准轴") && !feature.Name.Contains("点构线"))
                {
                    HOperatorSet.SmallestRectangle1(affine_Region1, out HTuple LL_LinEndY1, out HTuple LL_LinEndX1, out HTuple LL_LinStartY1, out HTuple LL_LinStartX1);
                    Algorithm.GetLine(image, 500, 10, LL_LinEndY1, LL_LinEndX1, LL_LinStartY1, LL_LinStartX1, out HTuple A11, out HTuple B11, out HTuple A21, out HTuple B21);

                    lineAndRecangelResults.Add(new LineAndRecangelResult() { name = feature.Name, startRow = A11.D, startCol = B11.D, endRow = A21.D, endCol = B21.D });
                }
                else if(feature.Name.Contains("椭圆") && !feature.Name.Contains("基准轴") && !feature.Name.Contains("点构线"))
                {
                    Algorithm.Get_Ellipes(image, affine_Region1, out HObject ellipesregion, out HTuple ellipesRow, out HTuple ellipesColumn, out HTuple RowL, out HTuple ColumnL, out HTuple RowR, out HTuple ColumnR);
                    HOperatorSet.SetColor(hSmartWindowControl1.HalconWindow, "blue");
                    HOperatorSet.SetDraw(hSmartWindowControl1.HalconWindow, "margin");
                    HOperatorSet.DispObj(ellipesregion, hSmartWindowControl1.HalconWindow);
                    lineAndRecangelResults.Add(new LineAndRecangelResult() { name = feature.Name, centerRow = ellipesRow.D, centerCol = ellipesColumn.D, startRow = RowL.D, startCol = ColumnL.D, endRow = RowR.D, endCol = ColumnR.D });
                }
            }
            catch (Exception ex)
            {


            }

        }
    }
    public class Measure_Tool
    {
        public string Name = "";
        public Measure_Style Measure_Style;
        public Feature MeasureFeature1 = new Feature();
        public Feature MeasureFeature2 = new Feature();
        public Feature MeasureFeature3 = new Feature();
        public Code_Style Code_Style;

        public double MaxValue = 0;
        public double MinValue = 0;
        public double SetValue = 0;//设定值
        public double CompensationValue = 0;//补偿
        public double CompensationValueLeft = 0;//补偿
        public double CompensationValueRight = 0;//补偿
        public double PositiveValue = 0;//正公差
        public double NegativeValue = 0;//负公差
        public double EllipseSetValueL = 0;//椭圆长设定值
        public double EllipseSetValueW = 0;//椭圆宽设定值
        public double EllipsePositiveValueL = 0;//椭圆长正公差
        public double EllipsePositiveValueW = 0;//椭圆宽正公差
        public double EllipseNegativeValueL = 0;//椭圆长负公差
        public double EllipseNegativeValueW = 0;//椭圆宽负公差
        public double EllipseMaxValueL = 0;//椭圆长上限
        public double EllipseMaxValueW = 0;//椭圆宽上限
        public double EllipseMinValueL = 0;//椭圆长下限
        public double EllipseMinValueW = 0;//椭圆宽下限
        public double EllipseCompensationValueL = 0;//补偿
        public double EllipseCompensationValueLeftL = 0;//补偿
        public double EllipseCompensationValueRightL = 0;//补偿
        public double EllipseCompensationValueW = 0;//补偿
        public double EllipseCompensationValueLeftW = 0;//补偿
        public double EllipseCompensationValueRightW = 0;//补偿


        public string PinName = "";
        public double PinMaxValueL = 0;
        public double PinMinValueL = 0;
        public double PinMaxValueW = 0;
        public double PinMinValueW = 0;
        public double PinMaxCircleX = 0;
        public double PinMinCircleX = 0;
        public double PinMaxCircleY = 0;
        public double PinMinCircleY = 0;
        public double PinSetValueL = 0;//长度设定值
        public double PinSetValueW = 0;//宽度设定值
        public double PinSetValueX = 0;//x轴设定值
        public double PinSetValueY = 0;//y轴设定值
        public double PinCompensationValueL = 0;//长补偿
        public double PinCompensationValueW = 0;//宽补偿
        public double PinCompensationValueX = 0;//X补偿
        public double PinCompensationValueY = 0;//Y补偿

        public double PinCompensationValueLeftL = 0;//长补偿
        public double PinCompensationValueRightL = 0;//长补偿
        public double PinCompensationValueLeftW = 0;//宽补偿
        public double PinCompensationValueRightW = 0;//宽补偿
        public double PinCompensationValueLeftX = 0;//x补偿
        public double PinCompensationValueRightX = 0;//x补偿
        public double PinCompensationValueLeftY = 0;//y补偿
        public double PinCompensationValueRightY = 0;//y补偿
        public double PinPositiveValueL = 0;//长正公差
        public double PinPositiveValueW = 0;//宽正公差
        public double PinNegativeValueL = 0;//长负公差
        public double PinNegativeValueW = 0;//宽负公差
        public double PinPositiveValueX = 0;//x正公差
        public double PinPositiveValueY = 0;//y正公差
        public double PinNegativeValueX = 0;//x负公差
        public double PinNegativeValueY = 0;//y负公差

        public Measure_Result Measure_Tool_Run(HSmartWindowControl hSmartWindowControl, HObject Image, HTuple manualImage_width, HTuple manualImage_Height, HTuple HomMat2DRotate, HTuple Calibtation, ref string Code,double Scaled)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            Measure_Result Result = new Measure_Result();
            HTuple Distance = new HTuple();
            HObject H_region1 = new HObject();
            HObject H_region2 = new HObject();
            HObject affine_Region1 = new HObject();
            HObject affine_Region2 = new HObject();
            HObject Circle1 = new HObject();
            HObject Circle2 = new HObject();
            HTuple Row1 = new HTuple();
            HTuple Row2 = new HTuple();
            HTuple Row3 = new HTuple();
            HTuple Column1 = new HTuple();
            HTuple Column2 = new HTuple();
            HTuple Column3 = new HTuple();
            Result.Measure_Name = Name;
            HTuple CodeStr = new HTuple();
            HObject symbolXLD = new HObject();
            HTuple Row = new HTuple();
            HTuple Column = new HTuple();
            HTuple PointOrder = new HTuple();
            HTuple Area = new HTuple();
            string CodeName = "";
            switch (Measure_Style)
            {
                case Measure_Style.Line_To_Line:
                    //第一条直线与第二条直线的交点

                    try
                    {
                        HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "green");


                        HOperatorSet.GenImageConst(out HObject StandraLine1, "byte", manualImage_width, manualImage_Height);
                        HOperatorSet.GenImageConst(out HObject StandraLine2, "byte", manualImage_width, manualImage_Height);
                        HOperatorSet.GenImageConst(out HObject StandraLine3, "byte", manualImage_width, manualImage_Height);

                        HOperatorSet.ReadRegion(out StandraLine1, MeasureFeature1.Region_Path);
                        HOperatorSet.ReadRegion(out StandraLine2, MeasureFeature2.Region_Path);
                        HOperatorSet.ReadRegion(out StandraLine3, MeasureFeature3.Region_Path);



                        HOperatorSet.AffineTransRegion(StandraLine1,out HObject regionAffineTrans1, HomMat2DRotate, "nearest_neighbor");
                        HOperatorSet.AffineTransRegion(StandraLine2,out HObject regionAffineTrans2, HomMat2DRotate, "nearest_neighbor");
                        HOperatorSet.AffineTransRegion(StandraLine3,out HObject regionAffineTrans3, HomMat2DRotate, "nearest_neighbor");



                        HOperatorSet.SmallestRectangle1(regionAffineTrans1, out HTuple LL_LinEndY1, out HTuple LL_LinEndX1, out HTuple LL_LinStartY1, out HTuple LL_LinStartX1);
                        HOperatorSet.SmallestRectangle1(regionAffineTrans2, out HTuple LL_LinEndY2, out HTuple LL_LinEndX2, out HTuple LL_LinStartY2, out HTuple LL_LinStartX2);
                        HOperatorSet.SmallestRectangle1(regionAffineTrans3, out HTuple LL_LinEndY3, out HTuple LL_LinEndX3, out HTuple LL_LinStartY3, out HTuple LL_LinStartX3);

                        Algorithm.GetLine(Image, 500, 10, LL_LinEndY1, LL_LinEndX1, LL_LinStartY1, LL_LinStartX1, out HTuple A11, out HTuple B11, out HTuple A21, out HTuple B21);
                        Algorithm.GetLine(Image, 500, 10, LL_LinEndY2, LL_LinEndX2, LL_LinStartY2, LL_LinStartX2, out HTuple A12, out HTuple B12, out HTuple A22, out HTuple B22);
                        Algorithm.GetLine(Image, 500, 10, LL_LinEndY3, LL_LinEndX3, LL_LinStartY3, LL_LinStartX3, out HTuple A13, out HTuple B13, out HTuple A23, out HTuple B23);



                        HOperatorSet.GenRegionLine(out HObject LL_line1, A11, B11, A21, B21);
                        HOperatorSet.GenRegionLine(out HObject LL_line2, A12, B12, A22, B22);
                        HOperatorSet.GenRegionLine(out HObject LL_line3, A13, B13, A23, B23);

                        HOperatorSet.DispRegion(LL_line2, hSmartWindowControl.HalconWindow);
                        HOperatorSet.DispRegion(LL_line3, hSmartWindowControl.HalconWindow);

                        HOperatorSet.IntersectionLines(A11, B11, A21, B21, A12, B12, A22, B22, out HTuple LL_Point_Row1, out HTuple LL_Point_Column1, out HTuple IsOverlapping1);
                        HOperatorSet.IntersectionLines(A12, B12, A22, B22, A13, B13, A23, B23, out HTuple LL_Point_Row2, out HTuple LL_Point_Column2, out HTuple IsOverlapping2);

                        HOperatorSet.GenCircle(out HObject LL_Circle1, LL_Point_Row1, LL_Point_Column1, 20);
                        HOperatorSet.GenCircle(out HObject LL_Circle2, LL_Point_Row2, LL_Point_Column2, 20);

                        HOperatorSet.GenRegionLine(out HObject LL_LineA, LL_Point_Row1, LL_Point_Column1, LL_Point_Row2, LL_Point_Column2);
                        HOperatorSet.DispRegion(LL_LineA, hSmartWindowControl.HalconWindow);

                        HOperatorSet.AffineTransPoint2d(Calibtation, LL_Point_Row1, LL_Point_Column1, out HTuple Cal_LL_Point_Column, out HTuple Cal_LL_Point_Row);
                        HOperatorSet.AffineTransPoint2d(Calibtation, LL_Point_Row2, LL_Point_Column2, out HTuple Cal_LL_ColProj, out HTuple Cal_LL_RowProj);

                        HOperatorSet.DistancePp(LL_Point_Row1, LL_Point_Column1, LL_Point_Row2, LL_Point_Column2, out HTuple LL_Distance1);
                        Console.WriteLine("两条直线像素距离：" + LL_Distance1.D); ;
                        HOperatorSet.DistancePp(Cal_LL_Point_Row, Cal_LL_Point_Column, Cal_LL_RowProj, Cal_LL_ColProj, out HTuple LL_Distance);


                        HOperatorSet.DispRegion(LL_Circle1, hSmartWindowControl.HalconWindow);
                        HOperatorSet.DispRegion(LL_Circle2, hSmartWindowControl.HalconWindow);





                        Result.Distance = LL_Distance.D;
                        if (LL_Distance.D > MinValue && LL_Distance.D < MaxValue)
                        {
                            HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "距离：" + LL_Distance.D.ToString("0.00"), "image", (LL_Point_Row1 + LL_Point_Row2) / 2, (LL_Point_Column1 + LL_Point_Column2) / 2, "green", "box", "false");
                            Result.Judge = true;
                        }
                        else
                        {
                            Result.Judge = false;
                            HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "距离：" + LL_Distance.D.ToString("0.00"), "image", (LL_Point_Row1 + LL_Point_Row2) / 2, (LL_Point_Column1 + LL_Point_Column2) / 2, "red", "box", "false");
                        }
                        StandraLine1.Dispose();
                        StandraLine2.Dispose();
                        StandraLine3.Dispose();
                        regionAffineTrans1.Dispose();
                        regionAffineTrans2.Dispose();
                        regionAffineTrans3.Dispose();
                        LL_line1.Dispose();
                        LL_line2.Dispose();
                        LL_line3.Dispose();
                        LL_Circle1.Dispose();
                        LL_Circle2.Dispose();
                        LL_LineA.Dispose();

                    }
                    catch
                    {
                        
                    }

                    

                    break;
                case Measure_Style.Point_To_Line:
                    HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "green");
                    HOperatorSet.GenImageConst(out H_region1, "byte", manualImage_width, manualImage_Height);
                    HOperatorSet.GenImageConst(out HObject P_L_StandraLine1, "byte", manualImage_width, manualImage_Height);

                    HOperatorSet.ReadRegion(out H_region1, MeasureFeature1.Region_Path);
                    HOperatorSet.ReadRegion(out P_L_StandraLine1, MeasureFeature2.Region_Path);

                    HOperatorSet.AffineTransRegion(H_region1, out  affine_Region1, HomMat2DRotate, "nearest_neighbor");


                   if(MeasureFeature1._Style == Feature_Style.Circle) Algorithm.Add_Measure_Point(Image, affine_Region1, out  Circle1, out Row1, out  Column1);
                   if(MeasureFeature1._Style == Feature_Style.SemiCircle) Algorithm.Add_Measure_SemiCircle(Image, affine_Region1, out Circle1, out Row1, out Column1);

                   
                    HOperatorSet.AffineTransRegion(P_L_StandraLine1, out HObject P_L_regionAffineTrans1, HomMat2DRotate, "nearest_neighbor");
                    HOperatorSet.SmallestRectangle1(P_L_regionAffineTrans1, out HTuple PL_LinStartY, out HTuple PL_LinStartX, out HTuple PL_LinEndY, out HTuple PL_LinEndX);

                    Algorithm.GetLine(Image, 500, 10, PL_LinStartY, PL_LinStartX, PL_LinEndY, PL_LinEndX,out HTuple A16, out HTuple B16, out HTuple A26, out HTuple B26);
                    HOperatorSet.GenRegionLine(out HObject PL_LineRegion11, A16, B16, A26, B26);


                    HOperatorSet.ProjectionPl(Row1, Column1, A16, B16, A26, B26, out HTuple RowProj, out HTuple ColProj);
                    HOperatorSet.DistancePp(RowProj, ColProj, Column1, Row1, out HTuple distance_pix);
                    Console.WriteLine("点到直线像素距离：" + distance_pix.D);

                    HOperatorSet.AffineTransPoint2d(Calibtation, Column1, Row1, out HTuple Calibtation_Column1_PL, out HTuple Calibtation_Row1_PL);
                    HOperatorSet.AffineTransPoint2d(Calibtation, ColProj, RowProj, out HTuple Calibtation_Column2_PL, out HTuple Calibtation_Row2_PL);

                    HOperatorSet.DistancePp(Calibtation_Row1_PL, Calibtation_Column1_PL, Calibtation_Row2_PL, Calibtation_Column2_PL, out HTuple PL_Distance);



                    HOperatorSet.GenRegionLine(out HObject PL_LineRegion, RowProj, ColProj, Row1, Column1);
                    HOperatorSet.GenCircle(out HObject PL_Circle, RowProj, ColProj, 20);
                    HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "green");
                    HOperatorSet.DispRegion(PL_LineRegion, hSmartWindowControl.HalconWindow);
                    HOperatorSet.DispRegion(PL_LineRegion11, hSmartWindowControl.HalconWindow);
                    HOperatorSet.DispRegion(PL_Circle, hSmartWindowControl.HalconWindow);

                    HOperatorSet.DispRegion(Circle1, hSmartWindowControl.HalconWindow);
                    Result.Distance = PL_Distance.D;
                    if (PL_Distance.D > MinValue && PL_Distance.D < MaxValue)
                    {
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "距离：" + PL_Distance.D.ToString("0.00"), "image", (RowProj + Row1) / 2, (Column1 + ColProj) / 2, "green", "box", "false");
                        Result.Judge = true;
                    }
                    else
                    {
                        Result.Judge = false;
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "距离：" + PL_Distance.D.ToString("0.00"), "image", (RowProj + Row1) / 2, (Column1 + ColProj) / 2, "red", "box", "false");
                    }
                    P_L_StandraLine1.Dispose();
                    P_L_regionAffineTrans1.Dispose();
                    PL_LineRegion11.Dispose();
                    PL_LineRegion.Dispose();
                    PL_Circle.Dispose();

                    break;
                case Measure_Style.Point_To_Point:
                    HOperatorSet.GenImageConst(out H_region1, "byte", manualImage_width, manualImage_Height);
                    HOperatorSet.GenImageConst(out H_region2, "byte", manualImage_width, manualImage_Height);

                    HOperatorSet.ReadRegion(out H_region1, MeasureFeature1.Region_Path);
                    HOperatorSet.ReadRegion(out H_region2, MeasureFeature2.Region_Path);

                    HOperatorSet.AffineTransRegion(H_region1, out  affine_Region1, HomMat2DRotate, "nearest_neighbor");
                    HOperatorSet.AffineTransRegion(H_region2, out  affine_Region2, HomMat2DRotate, "nearest_neighbor");


                    if (MeasureFeature1._Style == Feature_Style.Circle) Algorithm.Add_Measure_Point(Image, affine_Region1, out  Circle1, out  Row1, out  Column1);
                    if (MeasureFeature2._Style == Feature_Style.Circle) Algorithm.Add_Measure_Point(Image, affine_Region2, out  Circle2, out  Row2, out  Column2);

                    if (MeasureFeature1._Style == Feature_Style.SemiCircle) Algorithm.Add_Measure_SemiCircle(Image, affine_Region1, out Circle1, out Row1, out Column1);
                    if (MeasureFeature2._Style == Feature_Style.SemiCircle) Algorithm.Add_Measure_SemiCircle(Image, affine_Region2, out Circle2, out Row2, out Column2);

                    HOperatorSet.AffineTransPoint2d(Calibtation, Column1, Row1, out HTuple Calibtation_Column1, out HTuple Calibtation_Row1);
                    HOperatorSet.AffineTransPoint2d(Calibtation, Column2, Row2, out HTuple Calibtation_Column2, out HTuple Calibtation_Row2);

                    HOperatorSet.DistancePp(Calibtation_Row1, Calibtation_Column1, Calibtation_Row2, Calibtation_Column2, out  Distance);

                    HOperatorSet.SetColor(hSmartWindowControl.HalconWindow,"green");
                    HOperatorSet.GenRegionLine(out HObject LineRegion, Row1, Column1, Row2, Column2);
                    HOperatorSet.DispRegion(Circle1,hSmartWindowControl.HalconWindow);
                    HOperatorSet.DispRegion(Circle2, hSmartWindowControl.HalconWindow);
                    HOperatorSet.DispRegion(LineRegion, hSmartWindowControl.HalconWindow);

                    Result.Distance = Distance.D;

                    if (Distance.D>MinValue && Distance.D<MaxValue)
                    {
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "距离：" + Distance.D.ToString("0.00"), "image", (Row1 + Row2) / 2, (Column1 + Column2) / 2, "green", "box", "false");
                        Result.Judge = true;
                    }
                    else
                    {
                        Result.Judge = false;
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "距离：" + Distance.D.ToString("0.00"), "image", (Row1 + Row2) / 2, (Column1 + Column2) / 2, "red", "box", "false");
                    }

                    break;
                case Measure_Style.ReadCode:
                    HOperatorSet.GenImageConst(out H_region1, "byte", manualImage_width, manualImage_Height);
                    HOperatorSet.ReadRegion(out H_region1, MeasureFeature1.Region_Path);
                    HOperatorSet.AffineTransRegion(H_region1, out affine_Region1, HomMat2DRotate, "nearest_neighbor");

                    if (Code_Style == Code_Style.QR_Code1)
                    {
                        Algorithm.Read_Data_Code(Image, affine_Region1, out symbolXLD, out  CodeStr);
                        HOperatorSet.AreaCenterXld(symbolXLD, out  Area, out  Row, out  Column, out  PointOrder);
                        HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "blue");
                        HOperatorSet.DispXld(symbolXLD, hSmartWindowControl.HalconWindow);
                        CodeName = "二维码:";
                    }

                    if(Code_Style == Code_Style.bar_Code)
                    {
                        Algorithm.ReadBar_Code(Image, affine_Region1, out symbolXLD, out CodeStr);
                        HOperatorSet.AreaCenter(affine_Region1, out  Area, out Row, out Column);
                        HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "blue");
                        HOperatorSet.DispRegion(symbolXLD, hSmartWindowControl.HalconWindow);
                        CodeName = "条码:";
                    }


                    if (Code_Style == Code_Style.QR_Code2)
                    {
                        Algorithm.Read_Data_Code(Image, affine_Region1,out symbolXLD, out CodeStr);
                        HOperatorSet.AreaCenterXld(symbolXLD, out Area, out Row, out Column, out PointOrder);
                        HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "blue");
                        HOperatorSet.DispXld(symbolXLD, hSmartWindowControl.HalconWindow);
                        CodeName = "二维码:";
                    }




                    if (CodeStr.Length > 0)
                    {
                        string Temp_code = "";
                        if (CodeStr.S != "" && CodeStr.S != null)
                        {
                            Result.Judge = true;
                            HOperatorSet.DispText(hSmartWindowControl.HalconWindow, CodeName + CodeStr.S, "image", Row, Column, "red", "box", "false");
                            Code = CodeStr.S;
                            if (Code_Style != Code_Style.QR_Code2)
                            {
                                Result.Code = CodeStr.S;
                            }
                            else
                            {
                                string[] temp = Code.Split(';');
                                if(temp.Length>0)
                                {
                                    for(int i=0;i<temp.Length;i++)
                                    {
                                        if (temp[i].Contains("SN"))
                                        {
                                            Temp_code = temp[i];
                                            break;
                                        }

                                    }
                                }

                                if (Temp_code.Contains(":"))
                                {
                                    Result.Code = Temp_code.Split(':')[1];
                                }
                                else
                                {
                                    Result.Code = Temp_code;
                                }
                            }

                        }
                        else
                        {
    
                                Result.Judge = false;
             
                        }
                    }




                    break;
                case Measure_Style.Screw:
                    HOperatorSet.GenImageConst(out H_region1, "byte", manualImage_width, manualImage_Height);
                    HOperatorSet.ReadRegion(out H_region1, MeasureFeature1.Region_Path);
                    HOperatorSet.AffineTransRegion(H_region1, out affine_Region1, HomMat2DRotate, "nearest_neighbor");
                    HOperatorSet.AreaCenter(affine_Region1, out HTuple Affine_Area, out HTuple Affine_Row, out HTuple Affine_Column);

                    HOperatorSet.ReduceDomain(Image, affine_Region1, out HObject ImageReduced);
                    HOperatorSet.Threshold(ImageReduced, out HObject Region, 100, 255);
                    HOperatorSet.AreaCenter(Region, out HTuple Area_Screw, out HTuple Row_Scres, out HTuple Column_Scres);
                    if(Area_Screw>0)
                    {
                        Result.Judge = false;
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "螺丝缺失", "image", Affine_Row, Affine_Column, "red", "box", "false");
                    }
                    else
                    {
                        Result.Judge = true;
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "螺丝OK", "image", Affine_Row, Affine_Column, "green", "box", "false");
                    }
                    break;
                case Measure_Style.Circle_Measure:
                    HOperatorSet.GenImageConst(out H_region1, "byte", manualImage_width, manualImage_Height);
                    HOperatorSet.ReadRegion(out H_region1, MeasureFeature1.Region_Path);
                    HOperatorSet.AffineTransRegion(H_region1, out affine_Region1, HomMat2DRotate, "nearest_neighbor");
                    Algorithm.Get_Radius(Image, affine_Region1, out HObject Circle, out HTuple Radius,out HTuple Circle_Row,out HTuple Circle_Column);

                    double Radiu_Ca = Radius.D / Scaled;
                    Result.Distance = Radiu_Ca;


                    if (Radiu_Ca > MinValue && Radiu_Ca < MaxValue)
                    {
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "直径：" + Radiu_Ca.ToString("0.00"), "image", Circle_Row, Circle_Column, "green", "box", "false");
                        Result.Judge = true;
                        Result.Distance = Radiu_Ca;
                    }
                    else
                    {
                        Result.Judge = false;
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "直径：" + Radiu_Ca.ToString("0.00"), "image", Circle_Row, Circle_Column, "red", "box", "false");
                    }
                    break;
                case Measure_Style.Projection:
                    HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "green");
                    //画投影线
                    HOperatorSet.GenImageConst(out HObject Projection_Line, "byte", manualImage_width, manualImage_Height);
                    HOperatorSet.ReadRegion(out Projection_Line, MeasureFeature1.Region_Path);

                    HOperatorSet.AffineTransRegion(Projection_Line, out HObject Projection_AffineTrans1, HomMat2DRotate, "nearest_neighbor");
                    HOperatorSet.SmallestRectangle1(Projection_AffineTrans1, out HTuple Projection_LinEndY1, out HTuple Projection_LinEndX1, out HTuple Projection_LinStartY1, out HTuple Projection_LinStartX1);
                    Algorithm.GetLine(Image, 500, 10, Projection_LinEndY1, Projection_LinEndX1, Projection_LinStartY1, Projection_LinStartX1, out HTuple Projection_A11, out HTuple Projection_B11, out HTuple Projection_A21, out HTuple Projection_B21);
                    HOperatorSet.GenRegionLine(out HObject Projection_line1, Projection_A11, Projection_B11, Projection_A21, Projection_B21);

                    //画两个点
                    HOperatorSet.GenImageConst(out H_region1, "byte", manualImage_width, manualImage_Height);
                    HOperatorSet.GenImageConst(out H_region2, "byte", manualImage_width, manualImage_Height);

                    HOperatorSet.ReadRegion(out H_region1, MeasureFeature2.Region_Path);
                    HOperatorSet.ReadRegion(out H_region2, MeasureFeature3.Region_Path);

                    HOperatorSet.AffineTransRegion(H_region1, out affine_Region1, HomMat2DRotate, "nearest_neighbor");
                    HOperatorSet.AffineTransRegion(H_region2, out affine_Region2, HomMat2DRotate, "nearest_neighbor");


                    if (MeasureFeature2._Style == Feature_Style.Circle) Algorithm.Add_Measure_Point(Image, affine_Region1, out Circle1, out Row1, out Column1);
                    if (MeasureFeature3._Style == Feature_Style.Circle) Algorithm.Add_Measure_Point(Image, affine_Region2, out Circle2, out Row2, out Column2);

                    if (MeasureFeature2._Style == Feature_Style.SemiCircle) Algorithm.Add_Measure_SemiCircle(Image, affine_Region1, out Circle1, out Row1, out Column1);
                    if (MeasureFeature3._Style == Feature_Style.SemiCircle) Algorithm.Add_Measure_SemiCircle(Image, affine_Region2, out Circle2, out Row2, out Column2);


                    HOperatorSet.ProjectionPl(Row1, Column1, Projection_A11, Projection_B11, Projection_A21, Projection_B21, out HTuple RowProj1, out HTuple ColProj1);
                    HOperatorSet.ProjectionPl(Row2, Column2, Projection_A11, Projection_B11, Projection_A21, Projection_B21, out HTuple RowProj2, out HTuple ColProj2);

                    HOperatorSet.GenCircle(out HObject Projection_Point1, RowProj1, ColProj1, 50);
                    HOperatorSet.GenCircle(out HObject Projection_Point2, RowProj2, ColProj2, 50);

                    HOperatorSet.GenRegionLine(out HObject Projection_Line11, RowProj1, ColProj1, RowProj2, ColProj2);
                    HOperatorSet.GenRegionLine(out HObject Projection_Line20, Row1, Column1, RowProj1, ColProj1);
                    HOperatorSet.GenRegionLine(out HObject Projection_Line21, Row2, Column2, RowProj2, ColProj2);


                    HOperatorSet.AffineTransPoint2d(Calibtation, RowProj1, ColProj1, out HTuple Projection_Calibtation_Column1, out HTuple Projection_Calibtation_Row1);
                    HOperatorSet.AffineTransPoint2d(Calibtation, RowProj2, ColProj2, out HTuple Projection_Calibtation_Column2, out HTuple Projection_Calibtation_Row2);
                    HOperatorSet.DistancePp(Projection_Calibtation_Row1, Projection_Calibtation_Column1, Projection_Calibtation_Row2, Projection_Calibtation_Column2, out Distance);
                    Result.Distance = Distance.D;


                    HOperatorSet.DispRegion(Projection_Line11, hSmartWindowControl.HalconWindow);
                    HOperatorSet.DispRegion(Projection_Point1, hSmartWindowControl.HalconWindow);
                    HOperatorSet.DispRegion(Projection_Point2, hSmartWindowControl.HalconWindow);
                    HOperatorSet.DispRegion(Projection_Line20, hSmartWindowControl.HalconWindow);
                    HOperatorSet.DispRegion(Projection_Line21, hSmartWindowControl.HalconWindow);
                    HOperatorSet.DispRegion(Circle1, hSmartWindowControl.HalconWindow);
                    HOperatorSet.DispRegion(Circle2, hSmartWindowControl.HalconWindow);


                    if (Distance.D > MinValue && Distance.D < MaxValue)
                    {
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "距离：" + Distance.D.ToString("0.00"), "image", (RowProj1 + RowProj2) / 2, (ColProj1 + ColProj2) / 2, "green", "box", "false");
                        Result.Judge = true;
                    }
                    else
                    {
                        Result.Judge = false;
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "距离：" + Distance.D.ToString("0.00"), "image", (RowProj1 + RowProj2) / 2, (ColProj1 + ColProj2) / 2, "red", "box", "false");
                    }

                    Projection_Line.Dispose();
                    Projection_AffineTrans1.Dispose();
                    Projection_line1.Dispose();
                    Projection_Line11.Dispose();
                    Projection_Line20.Dispose();
                    Projection_Line21.Dispose();
                    Projection_Point1.Dispose();
                    Projection_Point2.Dispose();
                    break;
            }

            Distance.Dispose();
            H_region1.Dispose();
            H_region2.Dispose();
            affine_Region1.Dispose();
            affine_Region2.Dispose();
            Circle1.Dispose();
            Circle2.Dispose();
            Row1.Dispose();
            Row2.Dispose();
            Row3.Dispose();
            Column1.Dispose();
            Column2.Dispose();
            Column3.Dispose();
            CodeStr.Dispose();
            symbolXLD.Dispose();
            Row.Dispose();
            Column.Dispose();
            PointOrder.Dispose();
            Area.Dispose();
            stopwatch.Stop();
            Debug.WriteLine(Name+": "+ stopwatch.ElapsedMilliseconds);
            return Result;
        }
        public Measure_Result Measure_Tool_Run(HSmartWindowControl hSmartWindowControl, HObject Image, HTuple manualImage_width, HTuple manualImage_Height, HTuple HomMat2DRotate, HTuple Calibtation, ref string Code, double Scaled, List<LineAndRecangelResult> lineAndRecangelResults,String Manualpath)
        {
            //Stopwatch stopwatch = new Stopwatch();
            //stopwatch.Start();
            Measure_Result Result = new Measure_Result();
            HTuple Distance = new HTuple();
            HObject H_region1 = new HObject();
            HObject H_region2 = new HObject();
            HObject affine_Region1 = new HObject();
            HObject affine_Region2 = new HObject();
            HObject Circle1 = new HObject();
            HObject Circle2 = new HObject();
            HTuple Row1 = new HTuple();
            HTuple Row2 = new HTuple();
            HTuple Row3 = new HTuple();
            HTuple Column1 = new HTuple();
            HTuple Column2 = new HTuple();
            HTuple Column3 = new HTuple();
            Result.Measure_Name = Name;
            HTuple CodeStr = new HTuple();
            HObject symbolXLD = new HObject();
            HTuple Row = new HTuple();
            HTuple Column = new HTuple();
            HTuple PointOrder = new HTuple();
            HTuple Area = new HTuple();
            double DoubelDistance = 0;
            string StringDistance = "";
            double DoubelDistance1 = 0;
            string StringDistance1 = "";
            if (Manualpath =="Left")
            {
                CompensationValue = CompensationValueLeft;
                EllipseCompensationValueL = EllipseCompensationValueLeftL;
                EllipseCompensationValueW = EllipseCompensationValueLeftW;
            }
            if (Manualpath == "Right")
            {
                CompensationValue = CompensationValueRight;
                EllipseCompensationValueL = EllipseCompensationValueRightL;
                EllipseCompensationValueW = EllipseCompensationValueRightW;
            }
            string CodeName = "";
            try
            {


                switch (Measure_Style)
                {
                    case Measure_Style.Line_To_Line:
                        //第一条直线与第二条直线的交点

                        try
                        {
                            HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "green");

                            HTuple A11 = lineAndRecangelResults.Find(x => x.name == MeasureFeature1.Name).startRow;
                            HTuple B11 = lineAndRecangelResults.Find(x => x.name == MeasureFeature1.Name).startCol;
                            HTuple A21 = lineAndRecangelResults.Find(x => x.name == MeasureFeature1.Name).endRow;
                            HTuple B21 = lineAndRecangelResults.Find(x => x.name == MeasureFeature1.Name).endCol;
                            HTuple A12 = lineAndRecangelResults.Find(x => x.name == MeasureFeature2.Name).startRow;
                            HTuple B12 = lineAndRecangelResults.Find(x => x.name == MeasureFeature2.Name).startCol;
                            HTuple A22 = lineAndRecangelResults.Find(x => x.name == MeasureFeature2.Name).endRow;
                            HTuple B22 = lineAndRecangelResults.Find(x => x.name == MeasureFeature2.Name).endCol;
                            HTuple A13 = lineAndRecangelResults.Find(x => x.name == MeasureFeature3.Name).startRow;
                            HTuple B13 = lineAndRecangelResults.Find(x => x.name == MeasureFeature3.Name).startCol;
                            HTuple A23 = lineAndRecangelResults.Find(x => x.name == MeasureFeature3.Name).endRow;
                            HTuple B23 = lineAndRecangelResults.Find(x => x.name == MeasureFeature3.Name).endCol;


                            HOperatorSet.GenRegionLine(out HObject LL_line1, A11, B11, A21, B21);
                            HOperatorSet.GenRegionLine(out HObject LL_line2, A12, B12, A22, B22);
                            HOperatorSet.GenRegionLine(out HObject LL_line3, A13, B13, A23, B23);

                            HOperatorSet.DispRegion(LL_line2, hSmartWindowControl.HalconWindow);
                            HOperatorSet.DispRegion(LL_line3, hSmartWindowControl.HalconWindow);

                            HOperatorSet.IntersectionLines(A11, B11, A21, B21, A12, B12, A22, B22, out HTuple LL_Point_Row1, out HTuple LL_Point_Column1, out HTuple IsOverlapping1);
                            HOperatorSet.IntersectionLines(A12, B12, A22, B22, A13, B13, A23, B23, out HTuple LL_Point_Row2, out HTuple LL_Point_Column2, out HTuple IsOverlapping2);

                            HOperatorSet.GenCircle(out HObject LL_Circle1, LL_Point_Row1, LL_Point_Column1, 20);
                            HOperatorSet.GenCircle(out HObject LL_Circle2, LL_Point_Row2, LL_Point_Column2, 20);

                            HOperatorSet.GenRegionLine(out HObject LL_LineA, LL_Point_Row1, LL_Point_Column1, LL_Point_Row2, LL_Point_Column2);
                            HOperatorSet.DispRegion(LL_LineA, hSmartWindowControl.HalconWindow);

                            HOperatorSet.AffineTransPoint2d(Calibtation, LL_Point_Row1, LL_Point_Column1, out HTuple Cal_LL_Point_Column, out HTuple Cal_LL_Point_Row);
                            HOperatorSet.AffineTransPoint2d(Calibtation, LL_Point_Row2, LL_Point_Column2, out HTuple Cal_LL_ColProj, out HTuple Cal_LL_RowProj);

                            HOperatorSet.DistancePp(LL_Point_Row1, LL_Point_Column1, LL_Point_Row2, LL_Point_Column2, out HTuple LL_Distance1);
                            Console.WriteLine("两条直线像素距离：" + LL_Distance1.D); ;
                            HOperatorSet.DistancePp(Cal_LL_Point_Row, Cal_LL_Point_Column, Cal_LL_RowProj, Cal_LL_ColProj, out HTuple LL_Distance);


                            HOperatorSet.DispRegion(LL_Circle1, hSmartWindowControl.HalconWindow);
                            HOperatorSet.DispRegion(LL_Circle2, hSmartWindowControl.HalconWindow);




                            DoubelDistance = LL_Distance.D + CompensationValue;
                            StringDistance = DoubelDistance.ToString("0.00");
                            Result.Distance = Convert.ToDouble(StringDistance);
                            //Result.Distance = LL_Distance.D + CompensationValue;
                            if (Result.Distance >= MinValue && Result.Distance <= MaxValue)
                            {
                                HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "距离：" + Result.Distance.ToString("0.00"), "image", (LL_Point_Row1 + LL_Point_Row2) / 2, (LL_Point_Column1 + LL_Point_Column2) / 2, "green", "box", "false");
                                Result.Judge = true;
                            }
                            else
                            {
                                Result.Judge = false;
                                HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "距离：" + Result.Distance.ToString("0.00"), "image", (LL_Point_Row1 + LL_Point_Row2) / 2, (LL_Point_Column1 + LL_Point_Column2) / 2, "red", "box", "false");
                            }

                            LL_line1.Dispose();
                            LL_line2.Dispose();
                            LL_line3.Dispose();
                            LL_Circle1.Dispose();
                            LL_Circle2.Dispose();
                            LL_LineA.Dispose();

                        }
                        catch
                        {

                        }



                        break;
                    case Measure_Style.Point_To_Line:
                        HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "green");
                        string pointName = Name.Split(':')[1].Split('→')[0];
                        string lineName = Name.Split(':')[1].Split('→')[1];

                        Row1 = lineAndRecangelResults.Find(x => x.name == pointName).centerRow;
                        Column1 = lineAndRecangelResults.Find(x => x.name == pointName).centerCol;
                        double A16 = lineAndRecangelResults.Find(x => x.name == lineName).startRow;
                        double B16 = lineAndRecangelResults.Find(x => x.name == lineName).startCol;
                        double A26 = lineAndRecangelResults.Find(x => x.name == lineName).endRow;
                        double B26 = lineAndRecangelResults.Find(x => x.name == lineName).endCol;

                        HOperatorSet.GenRegionLine(out HObject PL_LineRegion11, A16, B16, A26, B26);

                        HOperatorSet.ProjectionPl(Row1, Column1, A16, B16, A26, B26, out HTuple RowProj, out HTuple ColProj);
                        HOperatorSet.DistancePp(RowProj, ColProj, Column1, Row1, out HTuple distance_pix);
                        Console.WriteLine("点到直线像素距离：" + distance_pix.D);

                        HOperatorSet.AffineTransPoint2d(Calibtation, Column1, Row1, out HTuple Calibtation_Column1_PL, out HTuple Calibtation_Row1_PL);
                        HOperatorSet.AffineTransPoint2d(Calibtation, ColProj, RowProj, out HTuple Calibtation_Column2_PL, out HTuple Calibtation_Row2_PL);

                        HOperatorSet.DistancePp(Calibtation_Row1_PL, Calibtation_Column1_PL, Calibtation_Row2_PL, Calibtation_Column2_PL, out HTuple PL_Distance);



                        HOperatorSet.GenRegionLine(out HObject PL_LineRegion, RowProj, ColProj, Row1, Column1);
                        HOperatorSet.GenCircle(out HObject PL_Circle, RowProj, ColProj, 20);
                        HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "green");
                        HOperatorSet.DispRegion(PL_LineRegion, hSmartWindowControl.HalconWindow);
                        HOperatorSet.DispRegion(PL_LineRegion11, hSmartWindowControl.HalconWindow);
                        HOperatorSet.DispRegion(PL_Circle, hSmartWindowControl.HalconWindow);

                        DoubelDistance = PL_Distance.D + CompensationValue;
                        StringDistance = DoubelDistance.ToString("0.00");
                        Result.Distance = Convert.ToDouble(StringDistance);
                        //Result.Distance = PL_Distance.D + CompensationValue;
                        if (Result.Distance >= MinValue && Result.Distance <= MaxValue)
                        {
                            HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "距离：" + Result.Distance.ToString("0.00"), "image", (RowProj + Row1) / 2, (Column1 + ColProj) / 2, "green", "box", "false");
                            Result.Judge = true;
                        }
                        else
                        {
                            Result.Judge = false;
                            HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "距离：" + Result.Distance.ToString("0.00"), "image", (RowProj + Row1) / 2, (Column1 + ColProj) / 2, "red", "box", "false");
                        }

                        PL_LineRegion11.Dispose();
                        PL_LineRegion.Dispose();
                        PL_Circle.Dispose();

                        break;
                    case Measure_Style.Point_To_Point:

                        HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "green");
                        string point1Name = Name.Split(':')[1].Split('→')[0];
                        string point2Name = Name.Split(':')[1].Split('→')[1];

                        Row1 = lineAndRecangelResults.Find(x => x.name == point1Name).centerRow;
                        Column1 = lineAndRecangelResults.Find(x => x.name == point1Name).centerCol;
                        Row2 = lineAndRecangelResults.Find(x => x.name == point2Name).centerRow;
                        Column2 = lineAndRecangelResults.Find(x => x.name == point2Name).centerCol;


                        HOperatorSet.AffineTransPoint2d(Calibtation, Column1, Row1, out HTuple Calibtation_Column1, out HTuple Calibtation_Row1);
                        HOperatorSet.AffineTransPoint2d(Calibtation, Column2, Row2, out HTuple Calibtation_Column2, out HTuple Calibtation_Row2);

                        HOperatorSet.DistancePp(Calibtation_Row1, Calibtation_Column1, Calibtation_Row2, Calibtation_Column2, out Distance);

                        HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "green");
                        HOperatorSet.GenRegionLine(out HObject LineRegion, Row1, Column1, Row2, Column2);
                        HOperatorSet.GenCircle(out Circle1, Row1, Column1, 10);
                        HOperatorSet.GenCircle(out Circle2, Row2, Column2, 10);
                        HOperatorSet.DispRegion(Circle1, hSmartWindowControl.HalconWindow);
                        HOperatorSet.DispRegion(Circle2, hSmartWindowControl.HalconWindow);
                        HOperatorSet.DispRegion(LineRegion, hSmartWindowControl.HalconWindow);

                        DoubelDistance = Distance.D + CompensationValue;
                        StringDistance= DoubelDistance.ToString("0.00");
                        Result.Distance = Convert.ToDouble(StringDistance);
                        //Result.Distance = Distance.D + CompensationValue;
                        if (Result.Distance >= MinValue && Result.Distance <= MaxValue)
                        {
                            HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "距离：" + Result.Distance.ToString("0.00"), "image", (Row1 + Row2) / 2, (Column1 + Column2) / 2, "green", "box", "false");
                            Result.Judge = true;
                        }
                        else
                        {
                            Result.Judge = false;
                            HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "距离：" + Result.Distance.ToString("0.00"), "image", (Row1 + Row2) / 2, (Column1 + Column2) / 2, "red", "box", "false");
                        }

                        break;
                    case Measure_Style.ReadCode:
                        HOperatorSet.GenImageConst(out H_region1, "byte", manualImage_width, manualImage_Height);
                        HOperatorSet.ReadRegion(out H_region1, MeasureFeature1.Region_Path);
                        HOperatorSet.AffineTransRegion(H_region1, out affine_Region1, HomMat2DRotate, "nearest_neighbor");

                        if (Code_Style == Code_Style.QR_Code1)
                        {
                            Algorithm.Read_Data_Code(Image, affine_Region1, out symbolXLD, out CodeStr);
                            HOperatorSet.AreaCenterXld(symbolXLD, out Area, out Row, out Column, out PointOrder);
                            HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "blue");
                            HOperatorSet.DispXld(symbolXLD, hSmartWindowControl.HalconWindow);
                            CodeName = "二维码:";
                        }

                        if (Code_Style == Code_Style.bar_Code)
                        {
                            Algorithm.ReadBar_Code(Image, affine_Region1, out symbolXLD, out CodeStr);
                            HOperatorSet.AreaCenter(affine_Region1, out Area, out Row, out Column);
                            HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "blue");
                            //HOperatorSet.DispRegion(symbolXLD, hSmartWindowControl.HalconWindow);
                            CodeName = "条码:";
                        }


                        if (Code_Style == Code_Style.QR_Code2)
                        {
                            Algorithm.Read_Data_Code(Image, affine_Region1, out symbolXLD, out CodeStr);
                            HOperatorSet.AreaCenterXld(symbolXLD, out Area, out Row, out Column, out PointOrder);
                            HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "blue");
                            HOperatorSet.DispXld(symbolXLD, hSmartWindowControl.HalconWindow);
                            CodeName = "二维码:";
                        }




                        if (CodeStr.Length > 0)
                        {
                            string Temp_code = "";
                            if (CodeStr.S != "" && CodeStr.S != null)
                            {
                                Result.Judge = true;
                                HOperatorSet.DispText(hSmartWindowControl.HalconWindow, CodeName + CodeStr.S, "image", Row, Column, "red", "box", "false");
                                Code = CodeStr.S;
                                if (Code_Style != Code_Style.QR_Code2)
                                {
                                    Result.Code = CodeStr.S;
                                }
                                else
                                {
                                    string[] temp = Code.Split(';');
                                    if (temp.Length > 0)
                                    {
                                        for (int i = 0; i < temp.Length; i++)
                                        {
                                            if (temp[i].Contains("SN"))
                                            {
                                                Temp_code = temp[i];
                                                break;
                                            }

                                        }
                                    }

                                    if (Temp_code.Contains(":"))
                                    {
                                        Result.Code = Temp_code.Split(':')[1];
                                    }
                                    else
                                    {
                                        Result.Code = Temp_code;
                                    }
                                }

                            }
                            else
                            {

                                Result.Judge = false;

                            }
                        }




                        break;
                    case Measure_Style.Screw:
                        HOperatorSet.GenImageConst(out H_region1, "byte", manualImage_width, manualImage_Height);
                        HOperatorSet.ReadRegion(out H_region1, MeasureFeature1.Region_Path);
                        HOperatorSet.AffineTransRegion(H_region1, out affine_Region1, HomMat2DRotate, "nearest_neighbor");
                        HOperatorSet.AreaCenter(affine_Region1, out HTuple Affine_Area, out HTuple Affine_Row, out HTuple Affine_Column);

                        HOperatorSet.ReduceDomain(Image, affine_Region1, out HObject ImageReduced);
                        HOperatorSet.Threshold(ImageReduced, out HObject Region, 100, 255);
                        HOperatorSet.AreaCenter(Region, out HTuple Area_Screw, out HTuple Row_Scres, out HTuple Column_Scres);
                        if (Area_Screw > 0)
                        {
                            Result.Judge = false;
                            HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "螺丝缺失", "image", Affine_Row, Affine_Column, "red", "box", "false");
                        }
                        else
                        {
                            Result.Judge = true;
                            HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "螺丝OK", "image", Affine_Row, Affine_Column, "green", "box", "false");
                        }
                        break;
                    case Measure_Style.Circle_Measure:
                        HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "green");
                        string pointName1 = Name.Split(':')[1].Split('→')[0];

                        HTuple Circle_Row = lineAndRecangelResults.Find(x => x.name == pointName1).centerRow;
                        HTuple Circle_Column = lineAndRecangelResults.Find(x => x.name == pointName1).centerCol;
                        HTuple Radius = lineAndRecangelResults.Find(x => x.name == pointName1).radius;

                        double Radiu_Ca = Radius.D / Scaled;
                        //Result.Distance = Radiu_Ca + CompensationValue;
                        DoubelDistance = Radiu_Ca + CompensationValue;
                        StringDistance = DoubelDistance.ToString("0.00");
                        Result.Distance = Convert.ToDouble(StringDistance);
                        if (Result.Distance >= MinValue && Result.Distance <= MaxValue)
                        {
                            HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "直径：" + Result.Distance.ToString("0.00"), "image", Circle_Row, Circle_Column, "green", "box", "false");
                            Result.Judge = true;
                            //Result.Distance = Radiu_Ca + CompensationValue;
                        }
                        else
                        {
                            Result.Judge = false;
                            HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "直径：" + Result.Distance.ToString("0.00"), "image", Circle_Row, Circle_Column, "red", "box", "false");
                        }
                        break;
                    case Measure_Style.Projection:
                        HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "green");
                        //画投影线

                        HTuple Projection_A11 = lineAndRecangelResults.Find(x => x.name == MeasureFeature1.Name).startRow;
                        HTuple Projection_B11 = lineAndRecangelResults.Find(x => x.name == MeasureFeature1.Name).startCol;
                        HTuple Projection_A21 = lineAndRecangelResults.Find(x => x.name == MeasureFeature1.Name).endRow;
                        HTuple Projection_B21 = lineAndRecangelResults.Find(x => x.name == MeasureFeature1.Name).endCol;
                        HOperatorSet.GenRegionLine(out HObject Projection_line1, Projection_A11, Projection_B11, Projection_A21, Projection_B21);

                        //画两个点
                        string point11Name = Name.Split(':')[1].Split('→')[0];
                        string point21Name = Name.Split(':')[1].Split('→')[1];

                        Row1 = lineAndRecangelResults.Find(x => x.name == point11Name).centerRow;
                        Column1 = lineAndRecangelResults.Find(x => x.name == point11Name).centerCol;
                        Row2 = lineAndRecangelResults.Find(x => x.name == point21Name).centerRow;
                        Column2 = lineAndRecangelResults.Find(x => x.name == point21Name).centerCol;
                        HOperatorSet.GenCircle(out Circle1, Row1, Column1, 10);
                        HOperatorSet.GenCircle(out Circle2, Row2, Column2, 10);



                        HOperatorSet.ProjectionPl(Row1, Column1, Projection_A11, Projection_B11, Projection_A21, Projection_B21, out HTuple RowProj1, out HTuple ColProj1);
                        HOperatorSet.ProjectionPl(Row2, Column2, Projection_A11, Projection_B11, Projection_A21, Projection_B21, out HTuple RowProj2, out HTuple ColProj2);

                        HOperatorSet.GenCircle(out HObject Projection_Point1, RowProj1, ColProj1, 50);
                        HOperatorSet.GenCircle(out HObject Projection_Point2, RowProj2, ColProj2, 50);

                        HOperatorSet.GenRegionLine(out HObject Projection_Line11, RowProj1, ColProj1, RowProj2, ColProj2);
                        HOperatorSet.GenRegionLine(out HObject Projection_Line20, Row1, Column1, RowProj1, ColProj1);
                        HOperatorSet.GenRegionLine(out HObject Projection_Line21, Row2, Column2, RowProj2, ColProj2);


                        HOperatorSet.AffineTransPoint2d(Calibtation, RowProj1, ColProj1, out HTuple Projection_Calibtation_Column1, out HTuple Projection_Calibtation_Row1);
                        HOperatorSet.AffineTransPoint2d(Calibtation, RowProj2, ColProj2, out HTuple Projection_Calibtation_Column2, out HTuple Projection_Calibtation_Row2);
                        HOperatorSet.DistancePp(Projection_Calibtation_Row1, Projection_Calibtation_Column1, Projection_Calibtation_Row2, Projection_Calibtation_Column2, out Distance);



                        HOperatorSet.DispRegion(Projection_Line11, hSmartWindowControl.HalconWindow);
                        HOperatorSet.DispRegion(Projection_Point1, hSmartWindowControl.HalconWindow);
                        HOperatorSet.DispRegion(Projection_Point2, hSmartWindowControl.HalconWindow);
                        HOperatorSet.DispRegion(Projection_Line20, hSmartWindowControl.HalconWindow);
                        HOperatorSet.DispRegion(Projection_Line21, hSmartWindowControl.HalconWindow);
                        HOperatorSet.DispRegion(Circle1, hSmartWindowControl.HalconWindow);
                        HOperatorSet.DispRegion(Circle2, hSmartWindowControl.HalconWindow);


                        //Result.Distance = Distance.D + CompensationValue;
                        DoubelDistance = Distance.D + CompensationValue;
                        StringDistance = DoubelDistance.ToString("0.00");
                        Result.Distance = Convert.ToDouble(StringDistance);
                        if (Result.Distance >= MinValue && Result.Distance <= MaxValue)
                        {
                            HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "距离：" + Result.Distance.ToString("0.00"), "image", (RowProj1 + RowProj2) / 2, (ColProj1 + ColProj2) / 2, "green", "box", "false");
                            Result.Judge = true;
                        }
                        else
                        {
                            Result.Judge = false;
                            HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "距离：" + Result.Distance.ToString("0.00"), "image", (RowProj1 + RowProj2) / 2, (ColProj1 + ColProj2) / 2, "red", "box", "false");
                        }

                        Projection_line1.Dispose();
                        Projection_Line11.Dispose();
                        Projection_Line20.Dispose();
                        Projection_Line21.Dispose();
                        Projection_Point1.Dispose();
                        Projection_Point2.Dispose();
                        break;
                    case Measure_Style.Ellipse:
                        HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "green");
                        string EllipseName1 = Name.Split(':')[1];

                        HTuple EllipseCircle_Row = lineAndRecangelResults.Find(x => x.name == EllipseName1).centerRow;
                        HTuple EllipseCircle_Column = lineAndRecangelResults.Find(x => x.name == EllipseName1).centerCol;
                        HTuple EllipseCircle_startRow = lineAndRecangelResults.Find(x => x.name == EllipseName1).startRow;
                        HTuple EllipseCircle_startCol = lineAndRecangelResults.Find(x => x.name == EllipseName1).startCol;
                        HTuple EllipseCircle_endRow = lineAndRecangelResults.Find(x => x.name == EllipseName1).endRow;
                        HTuple EllipseCircle_endCol = lineAndRecangelResults.Find(x => x.name == EllipseName1).endCol;

                        HOperatorSet.AffineTransPoint2d(Calibtation, EllipseCircle_startRow, EllipseCircle_startCol,  out HTuple Ellipse_startRow, out HTuple Ellipse_startColumn);
                        HOperatorSet.AffineTransPoint2d(Calibtation, EllipseCircle_endRow, EllipseCircle_endCol, out HTuple Ellipse_endRow, out HTuple Ellipse_endColumn);

                        double Length = Ellipse_startRow.D - Ellipse_endRow.D;
                        double Width = Ellipse_startColumn.D - Ellipse_endColumn.D;
                        if (Length<0)
                        {
                            Length = -Length;
                        }
                        if (Width<0)
                        {
                            Width = -Width;
                        }
                        if (Length< Width)
                        {
                            double Length1 = Length;
                            Length = Width;
                            Width = Length1;
                        }
                        //Result.Distance = Length + EllipseCompensationValueL;
                        DoubelDistance = Length + EllipseCompensationValueL;
                        StringDistance = DoubelDistance.ToString("0.00");
                        Result.Distance = Convert.ToDouble(StringDistance);

                        //Result.Width = Width + EllipseCompensationValueW;
                        DoubelDistance1 = Width + EllipseCompensationValueW;
                        StringDistance1 = DoubelDistance1.ToString("0.00");
                        Result.Width = Convert.ToDouble(StringDistance1);
                        if (Result.Distance >= EllipseMinValueL && Result.Distance <= EllipseMaxValueL)
                        {
                            HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "长度：" + Result.Distance.ToString("0.00"), "image", EllipseCircle_Row, EllipseCircle_Column, "green", "box", "false");
                            Result.Judge = true;
                            //Result.Distance = Radiu_Ca + CompensationValue;
                        }
                        else
                        {
                            Result.Judge = false;
                            HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "长度：" + Result.Distance.ToString("0.00"), "image", EllipseCircle_Row, EllipseCircle_Column, "red", "box", "false");
                        }
                        if (Result.Width >= EllipseMinValueW && Result.Width <= EllipseMaxValueW)
                        {
                            HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "直径：" + Result.Width.ToString("0.00"), "image", EllipseCircle_Row+200, EllipseCircle_Column, "green", "box", "false");
                            Result.Judge = true;
                            //Result.Distance = Radiu_Ca + CompensationValue;
                        }
                        else
                        {
                            Result.Judge = false;
                            HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "直径：" + Result.Width.ToString("0.00"), "image", EllipseCircle_Row+200, EllipseCircle_Column, "red", "box", "false");
                        }


                        break;
                }
            }
            catch (Exception ex)
            {


            }
            Distance.Dispose();
            H_region1.Dispose();
            H_region2.Dispose();
            affine_Region1.Dispose();
            affine_Region2.Dispose();
            Circle1.Dispose();
            Circle2.Dispose();
            Row1.Dispose();
            Row2.Dispose();
            Row3.Dispose();
            Column1.Dispose();
            Column2.Dispose();
            Column3.Dispose();
            CodeStr.Dispose();
            symbolXLD.Dispose();
            Row.Dispose();
            Column.Dispose();
            PointOrder.Dispose();
            Area.Dispose();
            //stopwatch.Stop();
            //Debug.WriteLine(Name + ": " + stopwatch.ElapsedMilliseconds);
            return Result;
        }
        public PinCheck MeasurePin(HSmartWindowControl hSmartWindowControl, HObject Image, HObject HikRegion, HTuple CameraParam, HTuple Pose, String Manualpath)
        {
            PinCheck pincheck = new PinCheck();
            if (Manualpath == "Left")
            {
                CompensationValue = CompensationValueLeft;
                PinCompensationValueL = PinCompensationValueLeftL;
                PinCompensationValueW = PinCompensationValueLeftW;
                PinCompensationValueX = PinCompensationValueLeftX;
                PinCompensationValueY = PinCompensationValueLeftY;
            }
            if (Manualpath == "Right")
            {
                CompensationValue = CompensationValueRight;
                PinCompensationValueL = PinCompensationValueRightL;
                PinCompensationValueW = PinCompensationValueRightW;
                PinCompensationValueX = PinCompensationValueRightX;
                PinCompensationValueY = PinCompensationValueRightY;
            }
            try
            {
                   Algorithm.MeasurePinRegion(Image, HikRegion, CameraParam, Pose, hSmartWindowControl.HalconWindow, out HTuple Height,
                       out HTuple Width, out HTuple Row, out HTuple Column, out HTuple Height1, out HTuple Width1, out HTuple Row1, out HTuple Column1, out HTuple Rowwy,
                       out HTuple Columnwy, out HTuple Rowwy1, out HTuple Columnwy1);
                if (Height1.D < Width1.D)
                {
                    pincheck.Height1 = Width1.D + PinCompensationValueW;
                    pincheck.Width1 = Height1.D+ PinCompensationValueL;
                    pincheck.Column1 = Row1.D+ PinCompensationValueX;
                    pincheck.Row1 =  Column1.D+ PinCompensationValueY;


                    pincheck.Width1 = Math.Round(pincheck.Width1, 2);
                    pincheck.Column1 = Math.Round(pincheck.Column1, 2);
                    pincheck.Row1 = Math.Round(pincheck.Row1, 2);
                    pincheck.Height1 = Math.Round(pincheck.Height1, 2);
                }
                else
                {
                    pincheck.Column1 = Column1.D + PinCompensationValueY;
                    pincheck.Row1 = Row1.D + PinCompensationValueX;
                    pincheck.Height1 = Height1.D + PinCompensationValueL;
                    pincheck.Width1 = Width1.D + PinCompensationValueW;

                    pincheck.Width1 = Math.Round(pincheck.Width1, 2);
                    pincheck.Column1 = Math.Round(pincheck.Column1, 2);
                    pincheck.Row1 = Math.Round(pincheck.Row1, 2);
                    pincheck.Height1 = Math.Round(pincheck.Height1, 2);
                }
                if (Height.D < Width.D)
                {
                    pincheck.Height = Width1.D + PinCompensationValueW;
                    pincheck.Width = Height1.D + PinCompensationValueL;
                    pincheck.Column = Row1.D + PinCompensationValueX;
                    pincheck.Row = Column1.D + PinCompensationValueY;

                    pincheck.Height = Math.Round(pincheck.Height, 2);
                    pincheck.Width = Math.Round(pincheck.Width, 2);
                    pincheck.Column = Math.Round(pincheck.Column, 2);
                    pincheck.Row = Math.Round(pincheck.Row, 2);
                }
                else
                {
                    pincheck.Column = Column.D + PinCompensationValueY;
                    pincheck.Row = Row.D + PinCompensationValueX;
                    pincheck.Height = Height.D + PinCompensationValueL;
                    pincheck.Width = Width.D + PinCompensationValueW;

                    pincheck.Height = Math.Round(pincheck.Height, 2);
                    pincheck.Width = Math.Round(pincheck.Width, 2);
                    pincheck.Column = Math.Round(pincheck.Column, 2);
                    pincheck.Row = Math.Round(pincheck.Row, 2);
                }

                if (pincheck.Width > PinMaxValueW || pincheck.Width < PinMinValueW|| pincheck.Width==0)
                    {
                        pincheck.TestReult = false;
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "         宽：" + pincheck.Width.ToString("0.00"), "image", Rowwy + 50, Columnwy + 32, "red", "box", "false");
                    }
                    else
                    {
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "             宽：" + pincheck.Width.ToString("0.00"), "image", Rowwy + 50, Columnwy + 32, "green", "box", "false");
                    }
                    if (pincheck.Height > PinMaxValueL || pincheck.Height < PinMinValueL|| pincheck.Height==0)
                    {
                        pincheck.TestReult = false;
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "         长：" + pincheck.Height.ToString("0.00"), "image", Rowwy, Columnwy + 32, "red", "box", "false");
                    }
                    else
                    {
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "         长：" + pincheck.Height.ToString("0.00"), "image", Rowwy, Columnwy + 32, "green", "box", "false");
                    }
                    if (pincheck.Row > PinMaxCircleX || pincheck.Row < PinMinCircleX|| pincheck.Row==0)
                    {
                        pincheck.TestReult = false;
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "水平偏差X：" + pincheck.Row.ToString("0.00"), "image", Rowwy + 100, Columnwy, "red", "box", "false");
                    }
                    else
                    {
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "水平偏差X：" + pincheck.Row.ToString("0.00"), "image", Rowwy + 100, Columnwy, "green", "box", "false");
                    }
                    if (pincheck.Column > PinMaxCircleY || pincheck.Column < PinMinCircleY|| pincheck.Column==0)
                    {
                        pincheck.TestReult = false;
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "水平偏差Y：" + pincheck.Column.ToString("0.00"), "image", Rowwy + 150, Columnwy, "red", "box", "false");
                    }
                    else
                    {
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "水平偏差Y：" + pincheck.Column.ToString("0.00"), "image", Rowwy + 150, Columnwy, "green", "box", "false");
                    }

                    //HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "    长：" + pincheck.Height.ToString("0.00")+"  宽：" + pincheck.Width.ToString("0.00")+"         " + "水平偏差X：" + pincheck.Row.ToString("0.00") + "  水平偏差Y：" + pincheck.Column.ToString("0.00"), "image", Rowwy, Columnwy, "green", "box", "false");
                    //HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "    长：" + pincheck1.Height.ToString("0.00") + "  宽：" + pincheck1.Width.ToString("0.00") + "         " + "水平偏差X：" + pincheck1.Row.ToString("0.00") + "  水平偏差Y：" + pincheck1.Column.ToString("0.00"), "image", Rowwy1, Columnwy1, "green", "box", "false");
                    if (pincheck.Width1 > PinMaxValueW || pincheck.Width1 < PinMinValueW|| pincheck.Width1==0)
                    {
                        pincheck.TestReult = false;
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "         宽：" + pincheck.Width1.ToString("0.00"), "image", Rowwy1 + 50, Columnwy1 + 32, "red", "box", "false");
                    }
                    else
                    {
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "         宽：" + pincheck.Width1.ToString("0.00"), "image", Rowwy1 + 50, Columnwy1 + 32, "green", "box", "false");
                    }
                    if (pincheck.Height1 > PinMaxValueL || pincheck.Height1 < PinMinValueL|| pincheck.Height1==0)
                    {
                        pincheck.TestReult = false;
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "         长：" + pincheck.Height1.ToString("0.00"), "image", Rowwy1, Columnwy1 + 32, "red", "box", "false");
                    }
                    else
                    {
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "         长：" + pincheck.Height1.ToString("0.00"), "image", Rowwy1, Columnwy1 + 32, "green", "box", "false");
                    }
                    if (pincheck.Row1 > PinMaxCircleX || pincheck.Row1 < PinMinCircleX|| pincheck.Row==0)
                    {
                        pincheck.TestReult = false;
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "水平偏差X：" + pincheck.Row1.ToString("0.00"), "image", Rowwy1 + 100, Columnwy1, "red", "box", "false");
                    }
                    else
                    {
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "水平偏差X：" + pincheck.Row1.ToString("0.00"), "image", Rowwy1 + 100, Columnwy1, "green", "box", "false");
                    }
                    if (pincheck.Column1 > PinMaxCircleY || pincheck.Column1 < PinMinCircleY|| pincheck.Column==0)
                    {
                        pincheck.TestReult = false;
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "水平偏差Y：" + pincheck.Column1.ToString("0.00"), "image", Rowwy1 + 150, Columnwy1, "red", "box", "false");
                    }
                    else
                    {
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, "水平偏差Y：" + pincheck.Column1.ToString("0.00"), "image", Rowwy1 + 150, Columnwy1, "green", "box", "false");
                    }
                    return pincheck;
            }
            catch (Exception ex)
            {
                pincheck.TestReult = false;
                return pincheck;
            }
        }
       
        public Calibration CalibrationLine(HSmartWindowControl hSmartWindowControl, HTuple A1, HTuple B1, HTuple A2, HTuple B2, HTuple A12, HTuple B12, HTuple A22, HTuple B22)
        {
            Calibration calibration = new Calibration();
            HOperatorSet.IntersectionLines(A1, B1, A2, B2, A12, B12, A22, B22, out HTuple Row, out HTuple Column, out HTuple IsOverlapping1);
            HOperatorSet.GenCircle(out HObject Circle, Row, Column, 20);
            HOperatorSet.DispRegion(Circle, hSmartWindowControl.HalconWindow);
            calibration.Row = Row.D;
            calibration.Column = Column.D;
            Circle.Dispose();
            Row.Dispose();
            Column.Dispose();
            return calibration;
        }


    }
    public enum Measure_Style
    {
        Point_To_Point = 0,
        Point_To_Line = 1,
        Line_To_Line = 2,
        Screw=3,
        ReadCode=4,
        Circle_Measure=5,
        PinCheck = 6,
        Projection=7,
        Ellipse=8   
    }

    public class H_Point
    {
        public double Row = 0;
        public double Column = 0;
    }
    public class PinCheck
    {
       public bool TestReult = true;
       public double Row = 0;
       public double Column = 0;
       public double Width = 0;
       public double Height = 0;

       public bool TestReult1 = true;
       public double Row1 = 0;
       public double Column1 = 0;
       public double Width1 = 0;
       public double Height1 = 0;
    }
    public class Calibration
    {
       public double Row = 0;
       public double Column = 0;

    }

    public static class OpearationTxt
    {
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string value, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        [DllImport("kernel32.dll")]
        static extern uint GetTickCount();


        /// <summary>  
        /// 读ini文件  
        /// </summary>  
        /// <param name="section">节</param>  
        /// <param name="key">键</param>  
        /// <param name="strPath">配置文件路径</param>  
        /// <returns></returns>  
        public static string ReadIni(string section, string key, string strPath)
        {
            try
            {
                StringBuilder temp = new StringBuilder(1024);
                int i = GetPrivateProfileString(section, key, "", temp, 1024, strPath);
                string result = temp.ToString();
                if (result.Contains("\\r\\n"))
                {
                    result = result.Replace(("\\r\\n"), ("\r\n"));
                }
                return result;
            }
            catch
            {
                return "";
            }

        }

        /// <summary>  
        /// 写入ini文件  
        /// </summary>  
        /// <param name="section">节</param>  
        /// <param name="key">键</param>  
        /// <param name="value">值</param>  
        /// <param name="strPath">文件路径</param>  
        /// <returns></returns>  
        public static void WriteIni(string section, string key, string value, string strPath)
        {
            if (value.Contains("\r\n"))
            {
                value = value.Replace(("\r\n"), ("\\r\\n"));
            }
            WritePrivateProfileString(section, key, value, strPath);
        }
        /// <summary>  
        /// 删除节  
        /// </summary>  
        /// <param name="section">节</param>  
        /// <param name="strPath"></param>  
        /// <returns></returns>  
        public static long DeleteSection(string section, string strPath)
        {
            return WritePrivateProfileString(section, null, null, strPath);
        }

        /// <summary>  
        /// 删除键  
        /// </summary>  
        /// <param name="section">节</param>  
        /// <param name="key">键</param>  
        /// <param name="strPath">文件路径</param>  
        /// <returns></returns>  
        public static long DeleteKey(string section, string key, string strPath)
        {
            return WritePrivateProfileString(section, key, null, strPath);
        }
    }

    public class ProductManager
    {
        public int ProductCount = 0;
        public int ProductOKCount = 0;
        public int ProductNGCount = 0;
        public string DataPath = "";
        public double ProductYield = 0;

        public ProductManager(string Data_path)
        {
            this.DataPath = Data_path;
            try
            {
                ProductCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductCount", DataPath));
                ProductOKCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductOKCount", DataPath));
                ProductNGCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductNGCount", DataPath));
            }
            catch
            {
                ProductCount = 0;
                ProductNGCount = 0;
                ProductOKCount = 0;

            }
        }
        public void NGAdd()
        {
            try
            {
                ProductCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductCount", DataPath));
                ProductOKCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductOKCount", DataPath));
                ProductNGCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductNGCount", DataPath));
            }
            catch
            {
                ProductCount = 0;
                ProductNGCount = 0;
                ProductOKCount = 0;

            }
            ProductCount++;
            ProductNGCount++;

            try
            {
                OpearationTxt.WriteIni(DateTime.Now.Date.ToString(), "ProductCount", ProductCount.ToString(), DataPath);
                OpearationTxt.WriteIni(DateTime.Now.Date.ToString(), "ProductOKCount", ProductOKCount.ToString(), DataPath);
                OpearationTxt.WriteIni(DateTime.Now.Date.ToString(), "ProductNGCount", ProductNGCount.ToString(), DataPath);
            }
            catch
            {
                Console.WriteLine("保存产能统计失败！");
            }

        }
        public void OKAdd()
        {
            try
            {
                ProductCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductCount", DataPath));
                ProductOKCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductOKCount", DataPath));
                ProductNGCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductNGCount", DataPath));
            }
            catch
            {
                ProductCount = 0;
                ProductNGCount = 0;
                ProductOKCount = 0;

            }
            ProductCount++;
            ProductOKCount++;

            try
            {
                OpearationTxt.WriteIni(DateTime.Now.Date.ToString(), "ProductCount", ProductCount.ToString(), DataPath);
                OpearationTxt.WriteIni(DateTime.Now.Date.ToString(), "ProductOKCount", ProductOKCount.ToString(), DataPath);
                OpearationTxt.WriteIni(DateTime.Now.Date.ToString(), "ProductNGCount", ProductNGCount.ToString(), DataPath);
            }
            catch
            {
                Console.WriteLine("保存产能统计失败！");
            }
        }

        public void TotalClear()
        {

            try
            {
                ProductCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductCount", DataPath));
                ProductOKCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductOKCount", DataPath));
                ProductNGCount = Convert.ToInt16(OpearationTxt.ReadIni(DateTime.Now.Date.ToString(), "ProductNGCount", DataPath));
            }
            catch
            {
                ProductCount = 0;
                ProductNGCount = 0;
                ProductOKCount = 0;

            }
            ProductCount = 0;
            ProductNGCount = 0;
            ProductOKCount = 0;

            try
            {
                OpearationTxt.WriteIni(DateTime.Now.Date.ToString(), "ProductCount", ProductCount.ToString(), DataPath);
                OpearationTxt.WriteIni(DateTime.Now.Date.ToString(), "ProductOKCount", ProductOKCount.ToString(), DataPath);
                OpearationTxt.WriteIni(DateTime.Now.Date.ToString(), "ProductNGCount", ProductNGCount.ToString(), DataPath);
            }
            catch
            {
                Console.WriteLine("保存产能统计失败！");
            }

        }

        public string Yield()
        {
            string result = "";
            if (ProductCount == 0)
            {
                result = "0";
            }
            if (ProductNGCount == 0 && ProductCount != 0)
            {
                result = "100%";
            }
            else if (ProductNGCount != 0)
            {
                result = (((double)ProductOKCount / (double)ProductCount) * 100).ToString("0.00") + "%";
            }
            else
            {
                result = "0";
            }
            return result;
        }
    }
}
